import pandas as pd
import requests
import lxml.etree as le
import openpyxl

# define variables to accept the target url and tag to be parsed
page_url = 'https://www.runoob.com/html/html-tutorial.html'
section = '//div[@id="leftcolumn"]/a/text()'
sectionLink = '//div[@id="leftcolumn"]/a/@href'

# use requests to get the web page code
content = requests.get(url=page_url).content
contentHtml = le.HTML(content)

# use xPath to get the data with the target tags
rets = contentHtml.xpath(section)
la = contentHtml.xpath(sectionLink)
data1 = []
data2 = []

# get the text in the tags
for leftColumn in rets:
    data1.append(leftColumn.strip())

for leftColumnURL in la:
    data2.append(page_url+leftColumnURL.strip())

# make the data frame to contain leftColumn and leftColumnURL
data = list(zip(data1, data2))
df = pd.DataFrame(data)
df.columns = ['section', 'sectionLink']
print(df)

# save the data frame to an excel file
writer = pd.ExcelWriter('runoob_html_leftcolumns.xlsx')
df.to_excel(writer, sheet_name='sectionAndLink')
writer.save()
